//分页插件
/**
2014-08-05 ch
**/
(function($) {
	var ms = {

		init: function(obj, args) {
			return (function() {
				ms.fillHtml(obj, args);
				ms.bindEvent(obj, args);
			})();
		},
		//填充html
		fillHtml: function(obj, args) {
			return (function() {
				obj.empty();
				//上一页
				if (args.current > 1) {
					obj.append('<a href="javascript:;" class="prevPage"> < </a>');
				} else {
					obj.remove('.prevPage');
					obj.append('<span class="disabled"> < </span>');
				}
				//中间页码

				//判断需要生成多少个页数号

				//已点击后面的页面
				if (args.current != 1 && args.current >= 3 && args.pageCount != 3) {
					obj.append('<a href="javascript:;" class="tcdNumber">' + 1 + '</a>');
				}

				//如果页数超过5，则当前页码在4之后，则前方开始隐藏
				if (args.current > 3 && args.current <= args.pageCount && args.pageCount > 4) {
					obj.append('<span>...</span>');
				}

				//判断页数的起始--从当前current前后2个
				var start = args.current - 1,
					end = args.current + 1;
				if ((start > 1 && args.current < 3) || args.current == 1) {
					end++;
				}

				//到达页尾，则
				if (args.current > args.pageCount - 3 && args.current >= args.pageCount) {
					start--;
				}

				//生成指定范围页码
				for (; start <= end; start++) { //展示的页数
					if (start <= args.pageCount && start >= 1) {
						if (start != args.current) {
							obj.append('<a href="javascript:;" class="tcdNumber">' + start + '</a>');
						} else {
							obj.append('<span class="current">' + start + '</span>'); //高亮当前页数
						}
					}
				}

				//增加省略号--如果pageCount大于5,还有2个空格
				if (args.current + 1 < args.pageCount - 1 && args.current >= 1 && args.pageCount > 4) {
					obj.append('<span>...</span>');
				}

				//设置结束页数号
				if (args.current != args.pageCount && args.current < args.pageCount - 1 && args.pageCount != 3) {
					obj.append('<a href="javascript:;" class="tcdNumber">' + args.pageCount + '</a>');
				}
				//下一页
				if (args.current < args.pageCount) {
					obj.append('<a href="javascript:;" class="nextPage"> > </a>');
				} else {
					obj.remove('.nextPage');
					obj.append('<span class="disabled"> > </span>');
				}
			})();
		},
		//绑定事件
		bindEvent: function(obj, args) {
			return (function() {
				//先进行解绑事件--防止页面多次生成翻页控件
				obj.off("click", "a.tcdNumber");
				obj.off("click", "a.prevPage");
				obj.off("click", "a.nextPage");

				//点击指定页码
				obj.on("click", "a.tcdNumber", function() {
					var current = parseInt($(this).text());
					ms.fillHtml(obj, {
						"current": current,
						"pageCount": args.pageCount
					});
					if (typeof(args.backFn) == "function") {
						args.backFn(current);
					}
				});
				//上一页
				obj.on("click", "a.prevPage", function() {
					var current = parseInt(obj.children("span.current").text());
					ms.fillHtml(obj, {
						"current": current - 1,
						"pageCount": args.pageCount
					});
					if (typeof(args.backFn) == "function") {
						args.backFn(current - 1);
					}
				});
				//下一页
				obj.on("click", "a.nextPage", function() {
					var current = parseInt(obj.children("span.current").text());
					ms.fillHtml(obj, {
						"current": current + 1,
						"pageCount": args.pageCount
					});
					if (typeof(args.backFn) == "function") {
						args.backFn(current + 1);
					}
				});
			})();
		}
	}
	$.fn.createPage = function(options) {
		var args = $.extend({
			pageCount: 10,
			current: 1,
			backFn: function() {}
		}, options);
		ms.init(this, args);
	}
})(jQuery);